package com.et114.modules.order.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.utility.StringPool;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.order.form.OrderForm;
import com.et114.modules.order.service.IMng_OrderService;
import com.et114.modules.order.service.Mng_OrderServiceImp;
import com.et114.modules.order.vo.OrderTrack;
import com.et114.modules.order.vo.OrderVO;
import com.et114.modules.org.services.UserManager;
import com.et114.modules.org.vo.UserVo;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.supplier.services.SupplierServiceImpl;
import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/**
 * 订单信息查询
 * 
 * @author zhiweip
 * @version 1.0
 * @created 09-十一月-2010 23:03:48
 */
public class Mng_OrderQueryAction extends BaseDispatchAction {
	
	private IMng_OrderService	mng_OrderService	= ( Mng_OrderServiceImp ) getBean ( "mng_OrderService" );
	private IMng_CustService mng_CustService = ( Mng_CustServiceImp )getBean ( "mng_CustService" ) ;
	
	@ Override
	public ActionForward unspecified ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		OrderForm cform = ( OrderForm ) form;
		
		OrderTrack vo = cform.getOrd ( );
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		//request.setAttribute ( "DataList" , mng_OrderService.queryOrderDataList ( vo ) );
		return mapping.findForward ( "queryOrderDataList" );
	}
	
	// Mng_CustForm
	public ActionForward orderQuery ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		OrderForm cform = ( OrderForm ) form;
		
		OrderTrack vo = cform.getOrd ( ); 
		vo.setPageNum ( cform.getPageNum ( ) );
		vo.setPageSize ( cform.getPageSize ( ) );
		String lang="zh";
		vo.setLanguage ( lang );
		String querystate=request.getParameter ( "querystate" );
		String yesorno=request.getParameter ( "yesorno" );
		if(querystate!=null&&querystate.length ( )>0){
			if(querystate.equalsIgnoreCase ( "isaudi" )){
				vo.setIsaudi ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "istosup" )){
				vo.setIstosup ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "islgi" )){
				vo.setIslgi ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "isarrive" )){
				vo.setIsarrive ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "isset" )){
				vo.setIsset ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "issettle" )){
				vo.setIssettle ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "iswork" )){
				vo.setIswork ( yesorno );
			}else if(querystate.equalsIgnoreCase ( "orderstateOK" )){ 
				
			}else if(querystate.equalsIgnoreCase ( "orderstate" )){
				vo.setOrderstate ( yesorno );
			}
			
		}
		request.setAttribute ( "querystate" , querystate );
		request.setAttribute ( "yesorno" , yesorno );
		request.setAttribute ( "ord" , vo );
		request.setAttribute ( "DataList" , mng_OrderService
				.queryOrderDataList ( vo ) );
		
		//String q=request.getRequestURL ( )+"#"+request.getQueryString ( );
		
		return mapping.findForward ( "queryOrderDataList" );
	}
	
	public ActionForward view ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		OrderForm cform = ( OrderForm ) form;
		OrderTrack vo = cform.getOrd ( );
		String lang="zh";
		vo.setLanguage ( lang );
		
		String orderid=request.getParameter ( "orderid" );
		if(orderid!=null)
			vo.setOrderid ( new Integer(orderid) );
		
		Object o=mng_OrderService.viewOrder ( vo); 
		Object info=mng_OrderService.loadOrderInfo ( vo );
		List<OrderTrack> dList=mng_OrderService.loadOrderDetail ( vo );
		
		request.setAttribute ( "ORDERDETAIL" ,dList );
		request.setAttribute ( "OrderBase" , o );
		request.setAttribute ( "OrderInfo" , info );
		
		if(o!=null){
			OrderTrack InfoVO=(OrderTrack)o;
			CustBaseVo cust=new CustBaseVo();
			cust.setCustid ( InfoVO.getCustid ( ) );
			request.setAttribute ( "BASEINFO" , mng_CustService .loadCust ( cust ) );
		}
		return mapping.findForward ( "detail" );
	}
	
	
	
	/**
	 * 商品类型
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward chooseTypes ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		request.setAttribute ( "OPTIONS" , mng_CustService.getItemTypes ( ) ) ;
		
		OrderForm cform = ( OrderForm ) form;
		String custid = request.getParameter ( "custid" );
		String opflag = request.getParameter ( "opflag" );
		
		if ( custid == null ) {
			
			if ( opflag != null && opflag.equals ( "fromCust" ) ) {
				request.setAttribute ( "msg" , "请选择一个客户!" );
				request.setAttribute ( "back" , "Mng_CustQueryAction.go" );
				return mapping.findForward ( "info" );
			}
			
		}
		request.setAttribute ( "custid" , custid );
		String comeFrom=request.getParameter ( "comeFrom" );
		if(comeFrom!=null)
		request.setAttribute ( "comeFrom" , comeFrom);
		
		return mapping.findForward ( "chooseTypes" );
	}
	
	public ActionForward inputOrder ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		
		OrderForm cform = ( OrderForm ) form;
		String orderid = request.getParameter ( "orderid" );
		String custid = request.getParameter ( "custid" );		
		String types = request.getParameter ( "types" );
		String detailId = request.getParameter ( "detailId" );
		
		String opflag = request.getParameter ( "opflag" );
		if(opflag!=null&&opflag.equalsIgnoreCase ( "fromModifyOrder" )){
			request.setAttribute ( "comeFrom" , "Mng_OrderModifyAction.go?method=toModify&orderid="+orderid );
			request.setAttribute ( "opflag" ,"fromModifyOrder");
		}
		
		OrderTrack paramVo=new OrderTrack();
		if(orderid!=null){
			paramVo.setOrderid ( new Integer(orderid) );
		
			OrderTrack po=(OrderTrack)mng_OrderService.loadOrder ( paramVo); 
			if(!po.getIsaudi ( ).equals ( "0" )){
				//request.setAttribute ( "msg" , "订单已审核,不能添加!" );
				//request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=orderQuery" );
				//return mapping.findForward ( "info" ) ;
			}
	
			custid =po.getCustid ( ).toString ( ); 
			types = po.getItemtype ( );
			
			request.setAttribute ( "ORDERID" , po.getOrderid ( ) );
			request.setAttribute ( "SUPID" , po.getSupid ( ) );
			request.setAttribute ( "ORDEMP" , po.getOrdemp ( ) );
			request.setAttribute ( "ORDERDATE" , po.getOrderdate ( ) );
			request.setAttribute ( "ORDERINFO" , po );
		}
		if(detailId!=null){
			paramVo=new OrderTrack();
			paramVo.setDetailid ( new Integer(detailId) );
			request.setAttribute ( "ORDERDETAIL" , mng_OrderService.loadOrderdetail ( paramVo ));
		}
		
		if ( custid == null ) {
			
			if ( opflag != null && opflag.equals ( "fromCust" ) ) {
				request.setAttribute ( "msg" , "请选择一个客户!" );
				request.setAttribute ( "back" , "Mng_CustQueryAction.go" );
				return mapping.findForward ( "info" );
			}
			
		}
		UserManager userManager = ( UserManager ) getBean ( "userManager" );
		UserVo user = new UserVo ( );
		user.setPageSize ( 10000 );
		request.setAttribute ( "EMPLYS" , ((com.et114.modules.cust.dao.Mng_CustDaoImp)getBean("mng_CustDao")).query_AllEmpList(null) );
		
		IMng_CustService mng_CustService = ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
		CustBaseVo cust = new CustBaseVo ( );
		cust.setCustid ( new Integer ( custid ) );
		request.setAttribute ( "BASEINFO" , mng_CustService.loadCust ( cust ) );
		
		
		char c = types.charAt ( 0 );
		request.setAttribute ( "TYPES" , types );
		//System.out.println ( "-----------------------" + types );
		switch ( c ) {
			case 'M' :

				SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
				ProSuppliers psvo = new ProSuppliers( ) ; 
				psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_NETBOOK + StringPool.COMMA ) ; 
				psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) ) ;
				List< ProSuppliers > proTypes = ssbean.querySuppliersList ( psvo ) ; 
				request.setAttribute ( "proTypes" , proTypes ) ;
				
				DictionaryCache bean = DictionaryCache.getInstance ( ); 
				List < BizOption > options = bean.listBizOptions (
						"BUYTYPE", false , CONST.COUNTRY_ZH );
				
				request.setAttribute ( "BTYPE" , options);
				
				return mapping.findForward ( "mobile_input" );
			case 'S' :
				return mapping.findForward ( "safe_input" );
			case 'C' :
				return mapping.findForward ( "cert_input" );
			case 'T' :
				return mapping.findForward ( "tv_input" );
			case 'P' :
				return mapping.findForward ( "pc_input" );
			default :
				return mapping.findForward ( "mobile_input" );
		}
		// return mapping.findForward ( "mobile_input" ) ;
	}
	
	public ActionForward addItem ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		OrderForm cform = ( OrderForm ) form;
		String orderid = request.getParameter ( "orderid" );
		OrderTrack vo = cform.getOrd ( );
		vo.setOrderid ( new Integer( orderid ) ); 
		vo=(OrderTrack)mng_OrderService.loadOrder ( vo );
		if(!vo.getIsaudi ( ).equals ( "0" )){
			request.setAttribute ( "msg" , "订单已审核,不能添加!" );
			request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=orderQuery" );
			return mapping.findForward ( "info" ) ;
		}
		response.sendRedirect ( "Mng_OrderQueryAction.go?method=inputOrder&orderid="+orderid+"&opflag=fromModifyOrder");
		
		return mapping.findForward ( "success" ) ;
	}
	
	public ActionForward udpInfo ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		request.setAttribute ( "EMPLYS" , ((com.et114.modules.cust.dao.Mng_CustDaoImp)getBean("mng_CustDao")).query_AllEmpList(null) );
		
		OrderForm cform = ( OrderForm ) form;
		String orderid = request.getParameter ( "orderid" );
		String custid = request.getParameter ( "custid" );
		OrderTrack vo = cform.getOrd ( );
		OrderTrack paramVo=new OrderTrack();
		String lang="zh";
		paramVo.setLanguage ( lang );
		if(orderid!=null){
			
			paramVo.setOrderid ( new Integer(orderid) );
			OrderTrack po=(OrderTrack)mng_OrderService.loadOrder ( paramVo);
			if(!po.getIsaudi ( ).equals ( "0" )){
				//request.setAttribute ( "msg" , "订单已审核,不能修改!" );
				//request.setAttribute ( "back" , "Mng_OrderQueryAction.go?method=orderQuery" );
				//return mapping.findForward ( "info" ) ;
			}
	
			custid =po.getCustid ( ).toString ( );
			request.setAttribute ( "ORDERID" , po.getOrderid ( ) );
			request.setAttribute ( "ITEMTYPE" , po.getItemtype ( ) );
			request.setAttribute ( "SUPID" , po.getSupid ( ) );
			request.setAttribute ( "ORDEMP" , po.getOrdemp ( ) );
			request.setAttribute ( "ORDERDATE" , po.getOrderdate ( ) );
			request.setAttribute ( "ORDERINFO" , po );
			request.setAttribute ( "OINFO" , mng_OrderService.loadOrderInfo ( paramVo));
			
		}
		CustBaseVo cust = new CustBaseVo ( );
		cust.setCustid ( new Integer ( custid ) );
		request.setAttribute ( "BASEINFO" , mng_CustService.loadCust ( cust ) );	
		
		String comeFrom=request.getParameter ( "comeFrom" );
		if(comeFrom!=null)
		request.setAttribute ( "comeFrom" , comeFrom+"&orderid="+orderid );
		
		return mapping.findForward ( "updInfo" ) ;
	}
	
}